package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SaveSpuVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author vvx
* @description
* @createDate 2023-08-15 18:49:15
*/

// ......................................重点
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService saleAttrService;
    @Autowired
    SpuSaleAttrValueService saleAttrValueService;

//      前端页面spu的保存
//    将穿过的vo封装到对应的数据库中

    @Override
    public void saveSpuInfo(SaveSpuVo vo) {

//        todo:1. 把基础信息 保存到 spu_info
//        todo 2.把图片 保存到 spu_image
//        todo 3.把销售属性存到 spu_sale_attr

//        1
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(vo, spuInfo);
//        Iservice
        spuInfoService.save(spuInfo);

//        自增id复用
        Long spuInfoId = spuInfo.getId();

//        2
        List<SpuImage> spuImages = vo.getSpuImageList().stream()
                .map(item -> {
//                    映射，新类封装
                    SpuImage spuImage = new SpuImage();
                    spuImage.setSpuId(spuInfoId);
                    spuImage.setImgName(item.getImgName());
                    spuImage.setImgUrl(item.getImgUrl());
                    return spuImage;
                }).collect(Collectors.toList());
//        图片连接，批量保存
        spuImageService.saveBatch(spuImages);

//        3.1 销售属性

        List<SpuSaleAttr> collect = vo.getSpuSaleAttrList().stream()
                .map(item -> {
                    SpuSaleAttr saleAttr = new SpuSaleAttr();
                    saleAttr.setSpuId(spuInfoId);
                    saleAttr.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    saleAttr.setSaleAttrName(item.getSaleAttrName());

                    return saleAttr;
                }).collect(Collectors.toList());
        //  insert into xxx(a,b,c) values (1,1,1),(2,2,3)
        saleAttrService.saveBatch(collect);


//        重点： Stream map嵌套 flatMap
//        3.2 销售值

        List<SpuSaleAttrValue> spuSaleAttrValues = vo.getSpuSaleAttrList().stream()
                .flatMap(item ->
                        item.getSpuSaleAttrValueList().stream().map(i -> {
                            i.setSaleAttrName(item.getSaleAttrName());
                            return i;
                        })
                )
                .map(item -> {
                    SpuSaleAttrValue attrValue = new SpuSaleAttrValue();

                    attrValue.setSpuId(spuInfoId);
                    attrValue.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    attrValue.setSaleAttrValueName(item.getSaleAttrValueName());
                    //冗余保存
                    attrValue.setSaleAttrName(item.getSaleAttrName());
                    return attrValue;
                }).collect(Collectors.toList());

        saleAttrValueService.saveBatch(spuSaleAttrValues);
    }

    /*
    * {
    "id": null,
    "spuName": "Test",
    "description": "test",
    "category3Id": 61,
    "spuImageList": [],
    "spuSaleAttrList": [
        {
            "baseSaleAttrId": "1",
            "saleAttrName": "选择颜色",
            "spuSaleAttrValueList": [
                {
                    "baseSaleAttrId": "1",
                    "saleAttrValueName": "1黑色"
                },
                {
                    "baseSaleAttrId": "1",
                    "saleAttrValueName": "2白色"
                },
                {
                    "baseSaleAttrId": "1",
                    "saleAttrValueName": "3华夏红"
                },
                {
                    "baseSaleAttrId": "1",
                    "saleAttrValueName": "4青漾"
                }
            ]
        },
        {
            "baseSaleAttrId": "2",
            "saleAttrName": "选择版本",
            "spuSaleAttrValueList": [
                {
                    "baseSaleAttrId": "2",
                    "saleAttrValueName": "8+128"
                },
                {
                    "baseSaleAttrId": "2",
                    "saleAttrValueName": "12+256"
                },
                {
                    "baseSaleAttrId": "2",
                    "saleAttrValueName": "12+512"
                }
            ]
        }
    ],
    "tmId": 2
}
    *
    * */
}




